package org.example.gateway.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class DebugRouteFilter implements GatewayFilter {
    
    private static final Logger log = LoggerFactory.getLogger(DebugRouteFilter.class);
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String path = exchange.getRequest().getURI().getPath();
        Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
        
        log.info("=== DebugRouteFilter ===");
        log.info("Request path: {}", path);
        log.info("Matched route: {}", route != null ? route.getId() : "NO ROUTE MATCHED");
        log.info("Route URI: {}", route != null ? route.getUri() : "NO URI");
        log.info("Route URI: {}", route != null ? route.getUri() : "NO URI");
        log.info("=======================");
        
        return chain.filter(exchange);
    }
}
